<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片布局区域 -->
    <el-card class="box-card">
      <!-- 搜索框区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="ordernum" placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表区域 -->
      <el-table :data="orderList" style="width: 100%" border stripe>
        <el-table-column align="center" type="index" label="#"></el-table-column>
        <el-table-column align="center" prop="order_number" label="订单编号" width="200"></el-table-column>
        <el-table-column align="center" prop="order_price" label="订单价格"></el-table-column>
        <el-table-column align="center" prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="is_send" label="是否发货"></el-table-column>
        <el-table-column align="center" prop="create_time" label="下单时间"></el-table-column>
        <el-table-column align="center" label="操作">
          <template>
            <el-button @click="showChangeAddressDialog" size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="showTime" size="mini" type="success" icon="el-icon-location"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="changeAddressDialog" width="50%" @close="changeAddressDialogClosed">
      <el-form :model="changeAddressForm" :rules="changeAddressFormRules" ref="changeAddressFormRef" label-width="100px">
        <el-form-item label="省市区/县" prop="address">
          <!-- <el-input v-model="changeAddressForm.address"></el-input> -->
          <el-cascader v-model="changeAddressForm.address" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addressInfo">
          <el-input v-model="changeAddressForm.addressInfo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="changeAddressDialog = false">取 消</el-button>
        <el-button type="primary" @click="changeAddressDialog = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度的弹框 -->
    <el-dialog title="物流进度" :visible.sync="processDialog" width="50%" @close="changeAddressDialogClosed">
      <el-timeline>
        <!-- timestamp 时间戳，是后台返回的时间 -->
        <!-- context 是后台返回的具体物流信息 -->
        <el-timeline-item v-for="(activity, index) in processList" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'

export default {
  name: 'OrderList',
  data() {
    return {
      // 订单号
      ordernum: '',
      // 请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单列表数据
      orderList: [],
      // 总数据条数
      total: 0,
      // 控制修改地址对话框的显示与隐藏
      changeAddressDialog: false,
      // 修改地址的数据对象
      changeAddressForm: {
        address: [],
        addressInfo: ''
      },
      // 修改地址的数据对象的验证规则
      changeAddressFormRules: {
        address: [{ required: true, message: '请选择省市区/县', trigger: 'blur' }],
        addressInfo: [{ required: true, message: '请填写详细地址', trigger: 'blur' }]
      },
      cityData: cityData,
      // 控制物流进度弹框的显示与隐藏
      processDialog: false,
      // 物流进度数据
      processList: []
    }
  },
  created() {
    // 调用获取订单列表数据的方法
    this.getOrderList()
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('/orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)

      res.data.goods.forEach(item => {
        item.create_time = this.$dayjs(item.create_time).format('YYYY-MM-DD hh:mm')
      })

      this.orderList = res.data.goods
      this.total = res.data.total
    },

    // 每页显示数据的条数（pagesize）发生改变时触发
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getOrderList()
    },
    // 当前页码（pagenum）发生改变时触发
    handleCurrentChange(pageNum) {
      this.queryInfo.pagenum = pageNum
      this.getOrderList()
    },

    // 展示修改地址弹框
    showChangeAddressDialog() {
      this.changeAddressDialog = true
    },
    // 关闭修改地址弹框，重置表单
    changeAddressDialogClosed() {
      this.$refs.changeAddressFormRef.resetFields()
    },

    // 展示物流进度弹框
    async showTime() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.processList = res.data
      this.processDialog = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
